package com.yitianyike.lockscreen.ces.bo.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.yitianyike.lockscreen.ces.bo.RecommendSubscribeListBO;
import com.yitianyike.lockscreen.ces.dao.ColumnDAO;
import com.yitianyike.lockscreen.ces.dao.PackageDAO;
import com.yitianyike.lockscreen.ces.model.Column;
import com.yitianyike.lockscreen.ces.model.PackageInfo;
import com.yitianyike.lockscreen.ces.model.response.Recommend;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Component("recommendSubscribeListBO")
public class RecommendSubscribeListBOImpl implements RecommendSubscribeListBO {
	@Autowired
	private PackageDAO packageDAO;
	@Autowired
	private ColumnDAO columnDAO;

	@Override
	public String organizeRecommendSubscribeList(Map<String, String> paramMap) {

		// List<Integer> recommendVersion =
		// packageDAO.getRecommendVersion(paramMap);
		// if (recommendVersion != null && !recommendVersion.isEmpty()) {
		// if (recommendVersion.get(0) == 2) {
		// Map<String, Object> recommendSubscribeMap = new HashMap<String,
		// Object>();
		// organizeRecommend2(recommendSubscribeMap, paramMap);
		// return JSONObject.fromObject(recommendSubscribeMap).toString();
		// } else {
		List<Map<String, Object>> recommendSubscribeList = new ArrayList<Map<String, Object>>();
		organizeRecommend1(recommendSubscribeList, paramMap);
		return JSONArray.fromObject(recommendSubscribeList).toString();
	}
	// }
	// return JSONObject.fromObject(new HashMap<String, Object>()).toString();

	// }

	private void organizeRecommend1(List<Map<String, Object>> recommendSubscribeList, Map<String, String> paramMap) {

		List<PackageInfo> packageList = packageDAO.getPackage(0, paramMap);
		List<Column> columnList = new ArrayList<Column>();
		Map<String, Object> map_p = new HashMap<String, Object>();
		map_p.put("pid", 0);
		map_p.put("name", "初始层");
		map_p.put("child", OrganizationChild(packageList, columnList));
		map_p.put("type", "0");
		recommendSubscribeList.add(map_p);
		OrganizationAll(recommendSubscribeList, packageList, columnList, paramMap);
		organizeRecommend2(recommendSubscribeList, paramMap);
	}

	private void organizeRecommend2(List<Map<String, Object>> recommendSubscribeList, Map<String, String> paramMap) {

		List<Integer> packageIdList = packageDAO.getPackageId(paramMap);
		List<Column> columnList = new ArrayList<Column>();
		if (!packageIdList.isEmpty()) {
			List<Column> columnLists = columnDAO.getColumnByPackageId(packageIdList, paramMap);
			columnList.addAll(columnLists);
		}
		Map<String, Object> map_p = new HashMap<String, Object>();
		map_p.put("pid", -1);
		map_p.put("name", "裸露");
		map_p.put("child", OrganizationChild(columnList));
		map_p.put("type", "0");
		recommendSubscribeList.add(map_p);
	}

	public List<Map<String, Object>> OrganizationChild(List<Column> listc) {
		List<Map<String, Object>> reslist = new ArrayList<Map<String, Object>>();

		if (listc != null) {
			for (Column column : listc) {
				Map<String, Object> list_action = new HashMap<String, Object>();
				Map<String, Object> preference = new HashMap<String, Object>();
				list_action.put("list_skip_url", column.getH5Url() + "");
				list_action.put("list_skip_action", column.getSkipAction() + "");
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("list_action", list_action);
				map.put("param", new ArrayList<Integer>());
				map.put("dataStyle", column.getColumnStyle());
				map.put("data", new ArrayList<Integer>());
				map.put("aid", column.getId() + "");
				map.put("pid", column.getPackageId());
				map.put("type", column.getType());
				map.put("little_type", column.getLittleType());
				map.put("level", 1);
				map.put("endTime", column.getEndTime());
				map.put("startTime", column.getStartTime());
				preference.put("icon", column.getIcon());
				preference.put("name", column.getColumnName());
				map.put("preference", preference);
				reslist.add(map);
			}

		}
		return reslist;
	}

	public void OrganizationAll(List<Map<String, Object>> recommendSubscribeList, List<PackageInfo> listp,
			List<Column> listc, Map<String, String> paramMap) {
		for (PackageInfo packageInfo : listp) {
			Map<String, Object> map_p = new HashMap<String, Object>();
			List<PackageInfo> packageList = packageDAO.getPackage(packageInfo.getPackageId(), paramMap);
			List<Column> columnList = columnDAO.getColumnByPackageId(packageInfo.getPackageId(), paramMap);
			if (packageList.isEmpty() && columnList.isEmpty()) {
				break;
			}
			map_p.put("pid", packageInfo.getPackageId());
			map_p.put("name", packageInfo.getPackageName());
			map_p.put("child", OrganizationChild(packageList, columnList));
			map_p.put("type", "1");
			recommendSubscribeList.add(map_p);
			if (!packageList.isEmpty()) {
				OrganizationAll(recommendSubscribeList, packageList, columnList, paramMap);
			}
		}
	}

	public List<Map<String, Object>> OrganizationChild(List<PackageInfo> listp, List<Column> listc) {
		List<Map<String, Object>> reslist = new ArrayList<Map<String, Object>>();
		if (listp != null) {
			for (PackageInfo packageInfo : listp) {
				Map<String, Object> list_action = new HashMap<String, Object>();
				Map<String, Object> preference = new HashMap<String, Object>();
				list_action.put("list_skip_url", "");
				list_action.put("list_skip_action", "");
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("list_action", list_action);
				map.put("param", new ArrayList<Integer>());
				map.put("dataStyle", packageInfo.getPackageStyle());
				map.put("data", new ArrayList<Integer>());
				map.put("aid", "");
				map.put("pid", packageInfo.getPackageId());
				map.put("type", 0);
				map.put("little_type", 0);
				map.put("level", 0);
				map.put("endTime", packageInfo.getEndTime());
				map.put("startTime", packageInfo.getStartTime());
				preference.put("icon", packageInfo.getIcon());
				preference.put("name", packageInfo.getPackageName());
				map.put("preference", preference);
				reslist.add(map);
			}
		}

		if (listc != null) {
			for (Column column : listc) {
				Map<String, Object> list_action = new HashMap<String, Object>();
				Map<String, Object> preference = new HashMap<String, Object>();
				list_action.put("list_skip_url", column.getH5Url() + "");
				list_action.put("list_skip_action", column.getSkipAction() + "");
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("list_action", list_action);
				map.put("param", new ArrayList<Integer>());
				map.put("dataStyle", column.getColumnStyle());
				map.put("data", new ArrayList<Integer>());
				map.put("aid", column.getId() + "");
				map.put("pid", column.getPackageId());
				map.put("type", column.getType());
				map.put("little_type", column.getLittleType());
				map.put("level", 1);
				map.put("endTime", column.getEndTime());
				map.put("startTime", column.getStartTime());
				preference.put("icon", column.getIcon());
				preference.put("name", column.getColumnName());
				map.put("preference", preference);
				reslist.add(map);
			}

		}
		return reslist;
	}

	private void organizeRecommend2(Map<String, Object> recommendSubscribeMap, Map<String, String> parmMap) {
		recommendSubscribeMap.put("top", organizeTop(parmMap));
		recommendSubscribeMap.put("middle", organizeMiddle(parmMap));
		recommendSubscribeMap.put("tail", organizeTail(parmMap));
	}

	private Object organizeTail(Map<String, String> parmMap) {

		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		List<Recommend> recommendList = packageDAO.organizeTail(parmMap);

		for (Recommend recommend : recommendList) {
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("name", recommend.getPackage_name());
			resultMap.put("order_no", recommend.getPackage_order_no());

			List<Map<String, Object>> resultColumnList = new ArrayList<Map<String, Object>>();
			Integer package_id = recommend.getPackage_id();
			parmMap.put("package_id", package_id.toString());
			List<Column> columnList = columnDAO.getColumnByPackageIdAndLimit(parmMap);
			for (Column column : columnList) {

				Map<String, Object> list_action = new HashMap<String, Object>();
				Map<String, Object> preference = new HashMap<String, Object>();
				list_action.put("list_skip_url", column.getH5Url());
				list_action.put("list_skip_action", column.getSkipAction());
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("list_action", list_action);
				String[] arr = null;
				map.put("param", arr);
				map.put("dataStyle", column.getColumnStyle());
				map.put("data", arr);
				map.put("aid", column.getId());
				map.put("pid", column.getPackageId());
				map.put("level", 1);
				map.put("endTime", column.getEndTime());
				map.put("startTime", column.getStartTime());
				preference.put("icon", column.getIcon());
				preference.put("name", column.getColumnName());
				map.put("preference", preference);
				resultColumnList.add(map);
			}
			resultMap.put("data", resultColumnList);
			resultList.add(resultMap);
		}
		return resultList;
	}

	private Object organizeMiddle(Map<String, String> parmMap) {
		List<Map<String, Object>> organizeMiddle = packageDAO.organizeMiddle(parmMap);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("count", organizeMiddle.size());
		resultMap.put("datas_middle", organizeMiddle);
		return resultMap;
	}

	private Object organizeTop(Map<String, String> parmMap) {
		List<Map<String, Object>> organizeTop = packageDAO.organizeTop(parmMap);
		return organizeTop;
	}

}
